I'm working on a simple sorting program where i need to sort an input from a user to remove whitespace, special character and sort the string(from user input) ascendingly. im new to learning js and i found this code online.
let sortNumButton = document.getElementById('sortNumButton');
let sortOutputContainer = document.getElementById('sortOutputContainer');
let inputField = document.getElementById('inputField');
sortNumButton.addEventListener('click', function(){
let paragraph = document.createElement('p')
paragraph.innerText = inputField.value.split('').sort().join('').replace(/[^a-zA-Z0-9-. ]/g, "");
sortOutputContainer.appendChild(paragraph);
inputField.value = "";
})
the program works but the output prints the number first, not alphabet first
You can .replace the result afterwards, matching numeric characters and putting that capture group at the end instead of the beginning.
const inputValue = "asd5lk43fj6vc";
const sorted = [...inputValue.replace(/[^a-zA-Z0-9-. ]/g, '')].sort().join('');
const result = sorted.replace(/^(\d+)(.*)/, '$2$1');
console.log(result);